package cn.com.zte.ngbc.frw.model;

import cn.com.zte.ngbc.frw.model.annotation.DefaultImplementation;
import cn.com.zte.ngbc.frw.model.dm.Options;
import cn.com.zte.ngbc.frw.model.dm.internal.DefaultOptions;
import cn.com.zte.ngbc.frw.model.util.ModelUtil;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;

public class ModelObjectCreator
{
  public static final ThreadLocal<Boolean> LazyResolve = new ThreadLocal()
  {
    protected Boolean initialValue()
    {
      return Boolean.valueOf(false);
    }
  };
  public static final ThreadLocal<Boolean> LazyLoad = new ThreadLocal()
  {
    protected Boolean initialValue()
    {
      return Boolean.valueOf(false);
    }
  };
  public static ModelObjectCreator instance = null;
  
  static
  {
    try
    {
      instance = (ModelObjectCreator)ModelUtil.classForName("cn.com.zte.ngbc.frw.model.util.ProxyModelObjectCreator").newInstance();
    }
    catch (Exception e)
    {
      instance = new ModelObjectCreator();
    }
  }
  
  public static ModelObjectCreator get()
  {
    return instance;
  }
  
  public <T> T create(Class<T> t)
  {
    t = resolveInterface(t);
    try
    {
      return (T)t.newInstance();
    }
    catch (Exception e)
    {
      throw new IllegalArgumentException(e);
    }
  }
  
  public <T> T createPageObj(Class<T> t)
  {
    t = resolveInterface(t);
    try
    {
      return (T)t.newInstance();
    }
    catch (Exception e)
    {
      throw new IllegalArgumentException(e);
    }
  }
  
  public <T> Class<T> resolveInterface(Class<T> type)
  {
    Class<?> classToCreate;

    if (type == Options.class)
    {
      classToCreate = DefaultOptions.class;
    }
    else
    {
 
      if ((type == List.class) || (type == Collection.class))
      {
        classToCreate = ArrayList.class;
      }
      else
      {
    
        if (type == Map.class)
        {
          classToCreate = HashMap.class;
        }
        else
        {
        
          if (type == SortedSet.class)
          {
            classToCreate = TreeSet.class;
          }
          else
          {
          
            if (type == Set.class)
            {
              classToCreate = HashSet.class;
            }
            else
            {
              DefaultImplementation dftImpl = (DefaultImplementation)type.getAnnotation(DefaultImplementation.class);
           
              if (dftImpl == null) {
                classToCreate = type;
              } else {
                try
                {
                  classToCreate = ModelUtil.classForName(dftImpl.value());
                }
                catch (Exception e)
                {
                  classToCreate = type;
                }
              }
            }
          }
        }
      }
    }
    return (Class<T>) classToCreate;
  }
  
  public <T> T createPageModel(Class<T> t)
  {
    t = resolveInterface(t);
    try
    {
      return (T)t.newInstance();
    }
    catch (Exception e)
    {
      throw new IllegalArgumentException(e);
    }
  }
  
  public <T> T cloneModel(T t)
  {
    return t;
  }
  
  public static abstract interface ModelClassAware
  {
    public abstract Class<?> getModelClass();
  }
}
